package com.ruoyi.verify.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.verify.domain.VerifyMemberInfo;
import com.ruoyi.verify.domain.VerifyPlanInfo;
import com.ruoyi.verify.domain.VerifyTemplate;
import com.ruoyi.verify.mapper.VerifyMemberInfoMapper;
import com.ruoyi.verify.mapper.VerifyTemplateMapper;
import com.ruoyi.verify.service.IVerifyMemberInfoService;
import com.ruoyi.verify.transform.FactoryInfo;
import com.ruoyi.verify.transform.VerifyPlanInfos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class VerifyMemberInfoServiceImpl implements IVerifyMemberInfoService
{
    @Autowired
    private VerifyMemberInfoMapper verifyMemberInfoMapper;

    @Autowired
    private VerifyTemplateMapper verifyTemplateMapper;



    /**
     * 查询【请填写功能名称】
     *
     * @param memberid 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public VerifyMemberInfo selectVerifyMemberInfoById(Long memberid)
    {
        return verifyMemberInfoMapper.selectVerifyMemberInfoById(memberid);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param verifyMemberInfo 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<VerifyMemberInfo> selectVerifyMemberInfoList(VerifyMemberInfo verifyMemberInfo)
    {
        return verifyMemberInfoMapper.selectVerifyMemberInfoList(verifyMemberInfo);
    }

    @Override
    public List<VerifyMemberInfo> selectVerifyMemberTeamList(VerifyMemberInfo verifyMemberInfo) {
//        verifyMemberInfo.setCurrentTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        return verifyMemberInfoMapper.selectVerifyMemberTeamList(verifyMemberInfo);
    }

    @Override
    public List<VerifyMemberInfo> selectVerifyTeamMemberInfoList(VerifyMemberInfo verifyMemberInfo) {
        return verifyMemberInfoMapper.selectVerifyTeamMemberInfoList(verifyMemberInfo);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param verifyMemberInfo 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertVerifyMemberInfo(VerifyMemberInfo verifyMemberInfo)
    {
        return verifyMemberInfoMapper.insertVerifyMemberInfo(verifyMemberInfo);
    }

    @Override
    public int insertVerifyTeamMember(VerifyMemberInfo verifyMemberInfo) {
        return verifyMemberInfoMapper.insertVerifyTeamMemberInfo(verifyMemberInfo);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param verifyMemberInfo 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateVerifyMemberInfo(VerifyMemberInfo verifyMemberInfo)
    {
        return verifyMemberInfoMapper.updateVerifyMemberInfo(verifyMemberInfo);
    }

    /**
     * 删除【请填写功能名称】对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteVerifyMemberInfoByIds(VerifyMemberInfo verifyMemberInfo)
    {
        return verifyMemberInfoMapper.deleteVerifyMemberInfoByIds(verifyMemberInfo);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param memberid 【请填写功能名称】ID
     * @return 结果
     */
    public int deleteVerifyMemberInfoById(Long memberid)
    {
        return verifyMemberInfoMapper.deleteVerifyMemberInfoById(memberid);
    }

    @Override
    public List<VerifyMemberInfo> selectVerifyMemberInfoByVerifytypeFactorycode(String verifytype, String factorycode) {
        return verifyMemberInfoMapper.selectVerifyMemberInfoByVerifytypeFactorycode(verifytype,factorycode);
    }

    @Override
    public int deleteVerifyTeamMemberInfoByVerifytypeFactorycode(String verifytype, String factorycode,String year) {
        return verifyMemberInfoMapper.deleteVerifyTeamMemberInfoByVerifytypeFactorycode(verifytype,factorycode,year);
    }

//    @Override
//    public List<VerifyMemberInfo> selectVerifyMemberInfoByPlanid(Long planid) {
//
//        return verifyMemberInfoMapper.selectVerifyMemberInfoByPlanid(planid);
//    }

    //返回成员信息
    @Override
    public List<VerifyPlanInfos.innerVerify> selectVerifyMemberInfoByPlanid(VerifyPlanInfo vpi) {
        List<VerifyMemberInfo> verifyMemberInfos = verifyMemberInfoMapper.selectVerifyMemberInfoByPlanid(vpi.getPlanid());
        List<VerifyPlanInfos.innerVerify> vpmm = verifyMemberInfos.stream().map(vmi -> {
            List<String> moudles = Arrays.asList(vmi.getMoudlecode().split(","));
            List<VerifyTemplate> verifyTemplates = null;
            List<VerifyTemplate> son_Ep6 = null;
            List<VerifyTemplate> Ep6 = null;

            //VDA6.3 删除P6的子模块列表
            if(vpi.getVerifytype().contains("VDA6.3")){
                verifyTemplates = verifyTemplateMapper.selectHistoryVerifyTemplateClassByPlanid(vpi.getPlanid());
                son_Ep6 = verifyTemplates.stream().filter(v -> v.getContent().startsWith("6.")).collect(Collectors.toList());
                List<String> ep6_verify_list = son_Ep6.stream().map(s -> s.getVerifyid()).collect(Collectors.toList());
                String ep6_string_list = StringUtils.join(ep6_verify_list, ",");
                Ep6 = verifyTemplates.stream().filter(v -> v.getContent().startsWith("6、")).collect(Collectors.toList());

                if(Ep6!=null && Ep6.size()>0){
                    if(vmi.getMoudlecode().contains(Ep6.get(0).getVerifyid())){
                        moudles = moudles.stream().filter(m -> !ep6_string_list.contains(m)).collect(Collectors.toList());
                    }
                }

            }

            if (vpi.getGroupleaderid() != null) {
                if (vpi.getGroupleaderid().equals(vmi.getMemberid())) {
                    vmi.setIsGroupLeader("1");
                } else {
                    vmi.setIsGroupLeader("0");
                }
            }
            VerifyPlanInfos.innerVerify innerVerify = new VerifyPlanInfos.innerVerify(vmi.getPlanid(), vmi.getMemberid(), moudles, vmi.getMembername(), vmi.getPhonenumber(), vmi.getJobnumber(), vmi.getJobunit(), vmi.getPosition(), vmi.getIsGroupLeader());
            return innerVerify;
        }).collect(Collectors.toList());
        return vpmm;
    }

    @Override
    public List<VerifyMemberInfo> selectVerifyMemberInfoByVerifytypeFactorycodeAndYear(String verifytype, String factorycode, String year) {
        return verifyMemberInfoMapper.selectVerifyMemberInfoByVerifytypeFactorycodeAndYear(verifytype,factorycode,year);
    }

    @Override
    public int updateVerifyTeamStatus(VerifyMemberInfo verifyMemberInfo) {
//        VerifyMemberInfo allverifyMemberInfo = new VerifyMemberInfo();
//        allverifyMemberInfo.setVerifytype(verifyMemberInfo.getVerifytype());
//        allverifyMemberInfo.setFactorycode(verifyMemberInfo.getFactorycode());
//        allverifyMemberInfo.setStatus("0");
//        verifyMemberInfoMapper.updateAllVerifyTeamStatus(allverifyMemberInfo);//不论年份，将此专家组状态全部设置为失效
        return verifyMemberInfoMapper.updateVerifyTeamStatus(verifyMemberInfo);//将当前年份专家组状态设置为生效
    }

    @Override
    public boolean ifTimeConflict(String verifytype, String factorycode,String starttime,String endtime) {
        return verifyMemberInfoMapper.ifTimeConflict(verifytype,factorycode,starttime,endtime);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int importVerifyTeamMemberInfo(VerifyMemberInfo verifyMemberInfo) {
        return verifyMemberInfoMapper.importVerifyTeamMemberInfo(verifyMemberInfo);
    }

    @Override
    public int deleteVerifyTeamMemberInfo(String jobnumber) {
        return verifyMemberInfoMapper.deleteVerifyTeamMemberInfo(jobnumber);
    }

    @Override
    public boolean ifHaveMemberInSrm(String jobnumber) {
        return verifyMemberInfoMapper.ifHaveMemberInSrm(jobnumber);
    }

    @Override
    public int copyVerifyTeam(VerifyMemberInfo verifyMemberInfo) {
        return verifyMemberInfoMapper.copyVerifyTeam(verifyMemberInfo);
    }

    @Override
    public VerifyMemberInfo selectVerifyExpertLibraryMember(String jobnumber) {
        return verifyMemberInfoMapper.selectVerifyExpertLibraryMember(jobnumber);
    }


    @Override
    public boolean ifHaveYearInSrm(String verifytype, String factorycode, String year) {
        return verifyMemberInfoMapper.ifHaveYearInSrm(verifytype,factorycode,year);
    }

    @Override
    public List<VerifyMemberInfo> selectVerifyMemberInfoByFactorycode(VerifyMemberInfo verifyMemberInfo) {
        return verifyMemberInfoMapper.selectVerifyMemberInfoByFactorycode(verifyMemberInfo);
    }

    @Override
    public List<VerifyMemberInfo> selectVerifyMemberInfoByMemberids(FactoryInfo factoryInfo) {
        return verifyMemberInfoMapper.selectVerifyMemberInfoByMemberids(factoryInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateVerifyMemberRole(String jobnumber, String roleid) {
        String userid = verifyMemberInfoMapper.selectVerifyUseridByLoginName(jobnumber);
        verifyMemberInfoMapper.deleteVerifyMemberRole(userid,roleid);
        verifyMemberInfoMapper.addVerifyMemberRole(userid,roleid);
    }

    @Override
    public List<String> selectAllVerifyMemberid(VerifyMemberInfo verifyMemberInfo) {
        return verifyMemberInfoMapper.selectAllVerifyMemberid(verifyMemberInfo);
    }


//    @Override
//    public int insertVerifyFactoryType(String verifytype, String factorycode, Long memberid) {
//
//        VerifyMemberFactoryType verifyMemberFactoryType = new VerifyMemberFactoryType();
//        verifyMemberFactoryType.setVerifytype(verifytype);
//        verifyMemberFactoryType.setFactorycode(factorycode);
//        verifyMemberFactoryType.setMemberid(memberid);
//        return verifyMemberFactoryTypeMapper.insertVerifyMemberFactoryType(verifyMemberFactoryType);
//    }


}
